In the last section, we have touched some basic concepts and usage methods of Socket. Then we have written Demo in a simple piggy chat room. I believe you have a preliminary grasp of Socket. In this section, we will learn how to use Socket to realize the intermittent transmission of large files. Here's an example of a big file uploaded by a Socket written by others. We don't need to write it by ourselves. We can use it when we need it.
1. Operation effect diagram:
1. First, we write a good Socket server to run:
2. Place an audio file in the SD card root directory:
3. Run our client:
4. After successful upload, you can see a file folder generated under the project of our server. We can find the uploaded file here:.Log is our log file.
2. Implementation flow chart:
3. Code examples:
Write a stream resolution class that both the server and the client will use:
StreamTool.java:
package com.test; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; public class StreamTool { public static void save(File file, byte[] data) throws Exception { FileOutputStream outStream = new FileOutputStream(file); outStream.write(data); outStream.close(); } public static String readLine(PushbackInputStream in) throws IOException { char buf[] = new char[128]; int room = buf.length; int offset = 0; int c; loop: while (true) { switch (c = in.read()) { case -1: case '\n': break loop; case '\r': int c2 = in.read(); if ((c2 != '\n') && (c2 != -1)) in.unread(c2); break loop; default: if (--room < 0) { char[] lineBuffer = buf; buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy(lineBuffer, 0, buf, 0, offset); } buf[offset++] = (char) c; break; } } if ((c == -1) && (offset == 0)) return null; return String.copyValueOf(buf, 0, offset); } /** * Read stream * * @param inStream * @return Byte array * @throws Exception */ public static byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); } }
1) Server implementation:
socket management and multithreading management classes:
FileServer.java:
package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PushbackInputStream; import java.io.RandomAccessFile; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FileServer { private ExecutorService executorService;// Thread pool private int port;// Listening port private boolean quit = false;// Sign out private ServerSocket server; private Map<Long, FileLog> datas = new HashMap<Long, FileLog>();// Store breakpoint data public FileServer(int port) { this.port = port; // Create a thread pool with (cpu number * 50) threads executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 50); } /** * Sign out */ public void quit() { this.quit = true; try { server.close(); } catch (IOException e) { } } /** * Start up service * * @throws Exception */ public void start() throws Exception { server = new ServerSocket(port); while (!quit) { try { Socket socket = server.accept(); // To support multi-user concurrent access, thread pool is used to manage the connection requests of each user. executorService.execute(new SocketTask(socket)); } catch (Exception e) { // e.printStackTrace(); } } } private final class SocketTask implements Runnable { private Socket socket = null; public SocketTask(Socket socket) { this.socket = socket; } public void run() { try { System.out.println("accepted connection " + socket.getInetAddress() + ":" + socket.getPort()); PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream()); // Get the first line of protocol data from the client: Content-Length = 143253434; filename = xxxx.3gp; sourceid= // If the user uploads the file for the first time, the value of sourceid is empty. String head = StreamTool.readLine(inStream); System.out.println(head); if (head != null) { // The following parameters are extracted from the protocol data String[] items = head.split(";"); String filelength = items[0].substring(items[0].indexOf("=") + 1); String filename = items[1].substring(items[1].indexOf("=") + 1); String sourceid = items[2].substring(items[2].indexOf("=") + 1); long id = System.currentTimeMillis();// Production resource id, if uniqueness is required, UUID can be used FileLog log = null; if (sourceid != null && !"".equals(sourceid)) { id = Long.valueOf(sourceid); log = find(id);// Find if the uploaded file has an upload record } File file = null; int position = 0; if (log == null) {// If no upload record exists, add a trace record to the file String path = new SimpleDateFormat("yyyy/MM/dd/HH/mm").format(new Date()); File dir = new File("file/" + path); if (!dir.exists()) dir.mkdirs(); file = new File(dir, filename); if (file.exists()) {// If the uploaded file is renamed, it is renamed filename = filename.substring(0, filename.indexOf(".") - 1) + dir.listFiles().length + filename.substring(filename.indexOf(".")); file = new File(dir, filename); } save(id, file); } else {// If there is an upload record, read the length of the uploaded data file = new File(log.getPath());// The path to get the file from the upload record if (file.exists()) { File logFile = new File(file.getParentFile(), file.getName() + ".log"); if (logFile.exists()) { Properties properties = new Properties(); properties.load(new FileInputStream(logFile)); position = Integer.valueOf(properties.getProperty("length"));// Read the length of uploaded data } } } OutputStream outStream = socket.getOutputStream(); String response = "sourceid=" + id + ";position=" + position + "\r\n"; // When the server receives the request information from the client, it returns the response information to the client: sourceid=1274773833264;position=0. // Sorceid is generated by the server and uniquely identifies the uploaded file. position indicates where the client starts uploading the file. outStream.write(response.getBytes()); RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd"); if (position == 0) fileOutStream.setLength(Integer.valueOf(filelength));// Set file length fileOutStream.seek(position);// Specifies to start writing data from a specific location of the file byte[] buffer = new byte[1024]; int len = -1; int length = position; while ((len = inStream.read(buffer)) != -1) {// Read and write data from input stream to file fileOutStream.write(buffer, 0, len); length += len; Properties properties = new Properties(); properties.put("length", String.valueOf(length)); FileOutputStream logFile = new FileOutputStream( new File(file.getParentFile(), file.getName() + ".log")); properties.store(logFile, null);// Real-time recording of received file length logFile.close(); } if (length == fileOutStream.length()) delete(id); fileOutStream.close(); inStream.close(); outStream.close(); file = null; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (socket != null && !socket.isClosed()) socket.close(); } catch (IOException e) { } } } } public FileLog find(Long sourceid) { return datas.get(sourceid); } // Save upload records public void save(Long id, File saveFile) { // It can be stored in a database in the future. datas.put(id, new FileLog(id, saveFile.getAbsolutePath())); } // When the file is uploaded, delete the record public void delete(long sourceid) { if (datas.containsKey(sourceid)) datas.remove(sourceid); } private class FileLog { private Long id; private String path; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public FileLog(Long id, String path) { this.id = id; this.path = path; } } }
Server-side interface class: ServerWindow.java:
public class ServerWindow extends Frame { private FileServer s = new FileServer(12345); private Label label; public ServerWindow(String title) { super(title); label = new Label(); add(label, BorderLayout.PAGE_START); label.setText("The server has been started"); this.addWindowListener(new WindowListener() { public void windowOpened(WindowEvent e) { new Thread(new Runnable() { public void run() { try { s.start(); } catch (Exception e) { // e.printStackTrace(); } } }).start(); } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void windowClosing(WindowEvent e) { s.quit(); System.exit(0); } public void windowClosed(WindowEvent e) { } public void windowActivated(WindowEvent e) { } }); } /** * @param args */ public static void main(String[] args) throws IOException { InetAddress address = InetAddress.getLocalHost(); ServerWindow window = new ServerWindow("File upload server:" + address.getHostAddress()); window.setSize(400, 300); window.setVisible(true); } }
2) Client (Android)
First, the layout file: activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="file name" android:textSize="18sp" /> <EditText android:id="@+id/edit_fname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Nikki Jamal - Priceless.mp3" /> <Button android:id="@+id/btn_upload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="upload" /> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop it" /> <ProgressBar android:id="@+id/pgbar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="fill_parent" android:layout_height="40px" /> <TextView android:id="@+id/txt_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" /> </LinearLayout>
Because of the breakpoint continuation, we need to save the upload progress, we need to use the database, here we define a database management class: DBOpenHelper.java:
Then there is the database operation class: UploadHelper.java:public class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(Context context) { super(context, "jay.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS uploadlog (_id integer primary key autoincrement, path varchar(20), sourceid varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
public class UploadHelper { private DBOpenHelper dbOpenHelper; public UploadHelper(Context context) { dbOpenHelper = new DBOpenHelper(context); } public String getBindId(File file) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select sourceid from uploadlog where path=?", new String[] { file.getAbsolutePath() }); if (cursor.moveToFirst()) { return cursor.getString(0); } return null; } public void save(String sourceid, File file) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.execSQL("insert into uploadlog(path,sourceid) values(?,?)", new Object[] { file.getAbsolutePath(), sourceid }); } public void delete(File file) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.execSQL("delete from uploadlog where path=?", new Object[] { file.getAbsolutePath() }); } }
By the way, don't forget that the client will also paste the stream resolution class, and finally our MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText edit_fname; private Button btn_upload; private Button btn_stop; private ProgressBar pgbar; private TextView txt_result; private UploadHelper upHelper; private boolean flag = true; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { pgbar.setProgress(msg.getData().getInt("length")); float num = (float) pgbar.getProgress() / (float) pgbar.getMax(); int result = (int) (num * 100); txt_result.setText(result + "%"); if (pgbar.getProgress() == pgbar.getMax()) { Toast.makeText(MainActivity.this, "Upload Success", Toast.LENGTH_SHORT).show(); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); upHelper = new UploadHelper(this); } private void bindViews() { edit_fname = (EditText) findViewById(R.id.edit_fname); btn_upload = (Button) findViewById(R.id.btn_upload); btn_stop = (Button) findViewById(R.id.btn_stop); pgbar = (ProgressBar) findViewById(R.id.pgbar); txt_result = (TextView) findViewById(R.id.txt_result); btn_upload.setOnClickListener(this); btn_stop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_upload: String filename = edit_fname.getText().toString(); flag = true; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = new File(Environment.getExternalStorageDirectory(), filename); if (file.exists()) { pgbar.setMax((int) file.length()); uploadFile(file); } else { Toast.makeText(MainActivity.this, "Documents do not exist~", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(MainActivity.this, "SD Card does not exist or is unavailable", Toast.LENGTH_SHORT).show(); } break; case R.id.btn_stop: flag = false; break; } } private void uploadFile(final File file) { new Thread(new Runnable() { public void run() { try { String sourceid = upHelper.getBindId(file); Socket socket = new Socket("172.16.2.54", 12345); OutputStream outStream = socket.getOutputStream(); String head = "Content-Length=" + file.length() + ";filename=" + file.getName() + ";sourceid=" + (sourceid != null ? sourceid : "") + "\r\n"; outStream.write(head.getBytes()); PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream()); String response = StreamTool.readLine(inStream); String[] items = response.split(";"); String responseSourceid = items[0].substring(items[0].indexOf("=") + 1); String position = items[1].substring(items[1].indexOf("=") + 1); if (sourceid == null) {//If the file is uploaded for the first time, the resource id bound by the file does not exist in the database. upHelper.save(responseSourceid, file); } RandomAccessFile fileOutStream = new RandomAccessFile(file, "r"); fileOutStream.seek(Integer.valueOf(position)); byte[] buffer = new byte[1024]; int len = -1; int length = Integer.valueOf(position); while (flag && (len = fileOutStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); length += len;//Accumulate the length of uploaded data Message msg = new Message(); msg.getData().putInt("length", length); handler.sendMessage(msg); } if (length == file.length()) upHelper.delete(file); fileOutStream.close(); outStream.close(); inStream.close(); socket.close(); } catch (Exception e) { Toast.makeText(MainActivity.this, "Upload anomaly~", Toast.LENGTH_SHORT).show(); } } }).start(); } }
Finally, remember to write these permissions to ** Android Manifest. XML **!
<! - Create and delete file permissions in SDCD --> ___________. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <! - Write data permissions to SDCard - > <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <! - Access to the internet - > <uses-permission android:name="android.permission.INTERNET"/>