1. 已知本地文件路徑,怎樣將圖片上傳到伺服器
一般採用的是FTP進行上傳就可以了!可以去伺服器廠商(正睿)的網上找找文件上傳的相關文檔參考一下,應該很快就清楚了!
2. php里,如何直接上傳圖片至專屬的圖片伺服器
生成一個隨機id 傳給B,隨機ID 存在A資料庫,B接受數據,讀取A的資料庫比對就行了。
資料庫僅僅存放那個id而已,相當於上傳時B問A,這個id是否合法,A說合法,就可以存圖片咯,B可以直接讀A的資料庫,也可以將這個id反傳給A,由A執行查詢告知B結果,用include就行,遠程包含一個A的php查詢頁面
3. android怎樣上傳圖片到伺服器
界面很簡單,點擊 【選擇圖片】,從圖庫里選擇圖片,顯示到下面的imageview里,點擊上傳,就會上傳到指定的伺服器
布局文件:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="選擇圖片"
android:id="@+id/selectImage"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="上傳圖片"
android:id="@+id/uploadImage"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
/>
</LinearLayout>
Upload Activity:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class Upload extends Activity implements OnClickListener {
private static String requestURL = "http://192.168.1.212:8011/pd/upload/fileUpload.do";
private Button selectImage, uploadImage;
private ImageView imageView;
private String picPath = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
selectImage = (Button) this.findViewById(R.id.selectImage);
uploadImage = (Button) this.findViewById(R.id.uploadImage);
selectImage.setOnClickListener(this);
uploadImage.setOnClickListener(this);
imageView = (ImageView) this.findViewById(R.id.imageView);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.selectImage:
/***
* 這個是調用android內置的intent,來過濾圖片文件 ,同時也可以過濾其他的
*/
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
break;
case R.id.uploadImage:
if (picPath == null) {
Toast.makeText(Upload.this, "請選擇圖片!", 1000).show();
} else {
final File file = new File(picPath);
if (file != null) {
String request = UploadUtil.uploadFile(file, requestURL);
uploadImage.setText(request);
}
}
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
/**
* 當選擇的圖片不為空的話,在獲取到圖片的途徑
*/
Uri uri = data.getData();
Log.e(TAG, "uri = " + uri);
try {
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, pojo, null, null, null);
if (cursor != null) {
ContentResolver cr = this.getContentResolver();
int colunm_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(colunm_index);
/***
* 這里加這樣一個判斷主要是為了第三方的軟體選擇,比如:使用第三方的文件管理器的話,你選擇的文件就不一定是圖片了,
* 這樣的話,我們判斷文件的後綴名 如果是圖片格式的話,那麼才可以
*/
if (path.endsWith("jpg") || path.endsWith("png")) {
picPath = path;
Bitmap bitmap = BitmapFactory.decodeStream(cr
.openInputStream(uri));
imageView.setImageBitmap(bitmap);
} else {
alert();
}
} else {
alert();
}
} catch (Exception e) {
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void alert() {
Dialog dialog = new AlertDialog.Builder(this).setTitle("提示")
.setMessage("您選擇的不是有效的圖片")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
picPath = null;
}
}).create();
dialog.show();
}
}
這個才是重點 UploadUtil:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public class UploadUtil {
private static final String TAG = "uploadFile";
private static final int TIME_OUT = 10 * 1000; // 超時時間
private static final String CHARSET = "utf-8"; // 設置編碼
/**
* 上傳文件到伺服器
* @param file 需要上傳的文件
* @param RequestURL 請求的rul
* @return 返回響應的內容
*/
public static int uploadFile(File file, String RequestURL) {
int res=0;
String result = null;
String BOUNDARY = UUID.randomUUID().toString(); // 邊界標識 隨機生成
String PREFIX = "--", LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; // 內容類型
try {
URL url = new URL(RequestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(TIME_OUT);
conn.setConnectTimeout(TIME_OUT);
conn.setDoInput(true); // 允許輸入流
conn.setDoOutput(true); // 允許輸出流
conn.setUseCaches(false); // 不允許使用緩存
conn.setRequestMethod("POST"); // 請求方式
conn.setRequestProperty("Charset", CHARSET); // 設置編碼
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="+ BOUNDARY);
if (file != null) {
/**
* 當文件不為空時執行上傳
*/
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
/**
* 這里重點注意: name裡面的值為伺服器端需要key 只有這個key 才可以得到對應的文件
* filename是文件的名字,包含後綴名
*/
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\""
+ file.getName() + "\"" + LINE_END);
sb.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINE_END);
sb.append(LINE_END);
dos.write(sb.toString().getBytes());
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
}
is.close();
dos.write(LINE_END.getBytes());
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)
.getBytes();
dos.write(end_data);
dos.flush();
/**
* 獲取響應碼 200=成功 當響應成功,獲取響應的流
*/
res = conn.getResponseCode();
Log.e(TAG, "response code:" + res);
if (res == 200) {
Log.e(TAG, "request success");
InputStream input = conn.getInputStream();
StringBuffer sb1 = new StringBuffer();
int ss;
while ((ss = input.read()) != -1) {
sb1.append((char) ss);
}
result = sb1.toString();
Log.e(TAG, "result : " + result);
} else {
Log.e(TAG, "request error");
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
}
4. java實現圖片上傳至伺服器並顯示,如何做
給你段代碼,是用來在ie上顯示圖片的(servlet):
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
File file = new File(getServletContext().getRealPath("/")+"out"+"/"+id+".gif");
response.setCharacterEncoding("gb2312");
response.setContentType("doc");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(file.getName().getBytes("gb2312"),"iso8859-1"));
System.out.println(new String(file.getName().getBytes("gb2312"),"gb2312"));
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int i = 0;
while((i = fis.read(b))!=-1)
{
output.write(b, 0, i);
}
output.write(b, 0, b.length);
output.flush();
response.flushBuffer();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
}
這個程序的功能是根據傳入的文件名(id),來為瀏覽器返回圖片流,顯示在<img>標簽里
標簽的格式寫成如下:
<img src="http://localhost:8080/app/preview?id=111 "/><br/>
顯示的是111.gif這個圖片
你上面的問題:
1.我覺得你的第二個辦法是對的,我們也是這樣做的,需要的是把資料庫的記錄id號傳進servlet,然後讀取這條記錄中的路徑信息,生成流以後返回就是了
關於上傳文件的問題,我記得java中應該專門有個負責文件上傳的類,你調用就行了,上傳後存儲在指定的目錄里,以實體文件的形式存放
你可以參考這個:
http://blog.csdn.net/arielxp/archive/2004/09/28/119592.aspx
回復:
1.是的,在response中寫入流就行了
2.是發到servlet中的,我們一般都是寫成servlet,短小精悍,使用起來方便,struts應該也可以,只是我沒有試過,恩,你理解的很對
5. struts2+Hibernate上傳圖片,如何實現圖片可以放在伺服器上;資料庫保存圖片路徑。
樓主
struts2
上傳代碼會寫嗎?
1.
struts2本身提供了上傳
攔截器
,用struts2實現上傳功能,並獲得保存地址
配置fileUpload的攔截器
2.
獲得文件存儲地址(上傳時指定)+文件名
3.
調用保存方法(
Hibernate
),將文件路徑存入資料庫
在一個事務中完成即可!
如:有哪塊有難點,及時追問。good
luck!
6. ueditor 圖片上傳到獨立的資源伺服器上,需要修改哪些配置,java項目
修改umeditor.config.js裡面的 window.UMEDITOR_CONFIG 的配置。
7. lua上傳圖片到伺服器
lua上傳圖片到伺服器的方法。
1、訪問前端伺服器。
2、修改restynginx的配置信息。
3、創建一個upfile.lua,放到指定位置。
4、創建一個html文件測試即可上傳圖片到伺服器。
8. markdown nginx 搭建自己的圖片伺服器
介紹
在使用markdown格式的過程中,經常需要上傳圖片,但是常常很復雜,image,在csdn上也很麻煩,在我有阿里雲的情況下,用nginx實現我的圖片伺服器.
安裝 OpenResty
OpenResty,以前用過,所以就按照文檔快速安裝.
apt-get install libpcre3-dev libssl-dev perl make build-essential curl
./configure
make
make install
默認目錄 :/usr/local/openresty/
添加配置文件
cd /usr/local/openresty/
mkdir conf/
vi nginx.conf
配置文件具體內容
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location ~ .*.(gif|jpg|jpeg|png)$ {
expires 24h;
root /home/images/;#指定圖片存放路徑
access_log /home/nginx/logs/images.log;#圖片 日誌路徑
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /home/images/;#代理臨時路徑
proxy_redirect off;
}
啟動
./openresty -c ../conf/nginx.conf
./openresty -s stop
netstat -antp
x
image
通過 SecureCRT 7.0拖拽上傳文件
image
通過 get -r * 同步文件到本地
image
訪問即可
https://blog.csdn.net/better_mouse/java/article/details/84256664
9. 伺服器配置,圖片和文件獨立放一個伺服器
很簡單的,你在圖片伺服器單獨架設個後台,只有上傳圖片功能,並返回圖片的URL地址。
然後在原來的後台上傳圖片那裡改為調用圖片伺服器的後台就可以了。
10. 如何上傳圖片到圖片伺服器
使用一些已有的組件幫助我們實現這種上傳功能。 常用的上傳組件: Apache 的 Commons FileUpload JavaZoom的UploadBean jspSmartUpload 以下,以FileUpload為例講解 1、在jsp端 <form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data"> 要注意enctype="multipart/form-data" 然後只需要放置一個file控制項,並執行submit操作即可 <input name="file" type="file" size="20" > <input type="submit" name="submit" value="提交" > 2、web端 核心代碼如下: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List items = upload.parseRequest(request); Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { System.out.println("表單參數名:" + item.getFieldName() + ",表單參數值:" + item.getString("UTF-8")); } else { if (item.getName() != null && !item.getName().equals("")) { System.out.println("上傳文件的大小:" + item.getSize()); System.out.println("上傳文件的類型:" + item.getContentType()); System.out.println("上傳文件的名稱:" + item.getName()); File tempFile = new File(item.getName()); File file = new File(sc.getRealPath("/") + savePath, tempFile.getName()); item.write(file); request.setAttribute("upload.message", "上傳文件成功!"); }else{ request.setAttribute("upload.message", "沒有選擇上傳文件!"); } } } }catch(FileUploadException e){ e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); request.setAttribute("upload.message", "上傳文件失敗!"); } request.getRequestDispatcher("/uploadResult.jsp").forward(request, response); }